from django.db import models
from django.contrib.auth.models import AbstractUser, Permission, Group

class AdminUser(AbstractUser):
    """管理员用户模型"""
    phone = models.CharField(max_length=20, blank=True, null=True, verbose_name="手机号")
    is_active = models.BooleanField(default=True, verbose_name="是否激活")
    last_login_ip = models.GenericIPAddressField(blank=True, null=True, verbose_name="最后登录IP")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    
    # 解决与User模型的冲突
    groups = models.ManyToManyField(
        Group,
        verbose_name="groups",
        blank=True,
        help_text="The groups this user belongs to.",
        related_name="adminuser_set",
        related_query_name="adminuser",
    )
    user_permissions = models.ManyToManyField(
        Permission,
        verbose_name="user permissions",
        blank=True,
        help_text="Specific permissions for this user.",
        related_name="adminuser_set",
        related_query_name="adminuser",
    )

    class Meta:
        verbose_name = "管理员"
        verbose_name_plural = "管理员"
        db_table = "admin_users"

    def __str__(self):
        return self.username


class AdminRole(models.Model):
    """管理员角色模型"""
    name = models.CharField(max_length=100, unique=True, verbose_name="角色名称")
    description = models.TextField(blank=True, null=True, verbose_name="描述")
    permissions = models.ManyToManyField(
        Permission, 
        blank=True,
        related_name="admin_roles",
        verbose_name="权限"
    )
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "管理员角色"
        verbose_name_plural = "管理员角色"

    def __str__(self):
        return self.name


class AdminUserRole(models.Model):
    """管理员用户角色关联模型"""
    admin_user = models.ForeignKey(
        AdminUser, 
        on_delete=models.CASCADE, 
        related_name="user_roles",
        verbose_name="管理员"
    )
    role = models.ForeignKey(
        AdminRole, 
        on_delete=models.CASCADE, 
        related_name="role_users",
        verbose_name="角色"
    )
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        verbose_name = "管理员-角色关联"
        verbose_name_plural = "管理员-角色关联"
        unique_together = ["admin_user", "role"]

    def __str__(self):
        return f"{self.admin_user.username} - {self.role.name}"


class AdminLoginLog(models.Model):
    """管理员登录日志"""
    admin_user = models.ForeignKey(
        AdminUser, 
        on_delete=models.CASCADE, 
        related_name="login_logs",
        verbose_name="管理员"
    )
    login_ip = models.GenericIPAddressField(verbose_name="登录IP")
    login_time = models.DateTimeField(auto_now_add=True, verbose_name="登录时间")
    login_status = models.BooleanField(default=True, verbose_name="登录状态")
    user_agent = models.TextField(blank=True, null=True, verbose_name="用户代理")

    class Meta:
        verbose_name = "管理员登录日志"
        verbose_name_plural = "管理员登录日志"
        ordering = ["-login_time"]

    def __str__(self):
        return f"{self.admin_user.username} - {self.login_time}"


class SystemConfig(models.Model):
    """系统配置模型"""
    key = models.CharField(max_length=100, unique=True, verbose_name="配置键")
    value = models.TextField(verbose_name="配置值")
    description = models.TextField(blank=True, null=True, verbose_name="描述")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "系统配置"
        verbose_name_plural = "系统配置"

    def __str__(self):
        return self.key
